package com.gexiaobao.pigeon.viewmodel

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.LoginInfoResponse
import com.gexiaobao.pigeon.app.model.bean.OrganizationIdResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.GetVerCodeParams
import com.gexiaobao.pigeon.app.model.param.LoginCodeParams
import com.gexiaobao.pigeon.app.model.param.LoginParams
import com.gexiaobao.pigeon.app.model.param.RegisterParams
import com.gexiaobao.pigeon.app.model.param.ResetPwdParams
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 * created by : hxw
 * @date : 20221121
 * Describe :
 */
class LoginViewModel : BaseViewModel() {

    val username = StringObservableField()
    val password = StringObservableField()
    val hintUserName = StringObservableField()

    /**  */
    //手机号
    var phoneNum = StringObservableField()

    //身份证
    var IDNumber = StringObservableField()

    //真实姓名
    var userRealMame = StringObservableField()

    //验证码
    var verificationCode = StringObservableField()

    //获取验证码
    var getVerificationCode = StringObservableField()

    //密码(登录注册界面)
    var password2 = StringObservableField()

    //注册选择省市区
    val address = StringObservableField()

    //注册填写详细地址
    val detailAddress = StringObservableField()

    var mResultReSet = MutableLiveData<ResultState<Any>>()
    var mResultRegister: MutableLiveData<UiState> = MutableLiveData()

    //获取组织id
    var mOrgIdResult: MutableLiveData<OrganizationIdResponse> = MutableLiveData()

    //    var  loginResult= MutableLiveData<ResultState<LoginInfoResponse>>(
    val loginResult: MutableLiveData<LoginInfoResponse> = MutableLiveData()

    /** 发送验证码 */
    val verCodeResult: MutableLiveData<UiState> = MutableLiveData()

    //用户名清除按钮是否显示
    var clearVisible = object : ObservableInt(username) {
        override fun get(): Int {
            return if (username.get().isEmpty()) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
    }

    //密码显示按钮是否显示
    var passwordVisible = object : ObservableInt(password) {
        override fun get(): Int {
            return if (password.get().isEmpty()) {
                View.GONE
            } else {
                View.VISIBLE
            }
        }
    }

    fun login(body: LoginParams) {
        request(
            { apiService.login(body) }, {
                loginResult.value = it//请求的返回结果，请求成功与否都会改变该值，在Activity或fragment中监听回调结果，具体可看loginActivity中的回调
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    fun codeLogin(body: LoginCodeParams) {
        request(
            { apiService.codeLogin(body) }, {
                loginResult.value = it//请求的返回结果，请求成功与否都会改变该值，在Activity或fragment中监听回调结果，具体可看loginActivity中的回调
            }, {
                RxToast.showToast(it.msg)
            }
        )
    }

    fun getVerCode(body: GetVerCodeParams) {
        request({ apiService.getVerCode(body) }, {
            val result = UiState(isSuccess = true)
            verCodeResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            verCodeResult.value = result
        })
    }

    /** 重置密码 */
    fun reSetPwd(body: ResetPwdParams) {
        request({ apiService.reSetPwd(body) }, mResultReSet, true, appContext.getString(R.string.loading))
    }


    fun register(body: RegisterParams) {
        request({ apiService.register(body) }, {
            val result = UiState(isSuccess = true)
            mResultRegister.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            mResultRegister.value = result
        })
    }

    /** 获取放飞员组织id */
    fun getOrg() {
        request({ apiService.getOrg() }, {
            mOrgIdResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

}